﻿using LD.Controls;
using LD.Host;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace LD
{
    public class MenuLocation
    {
        public int Left = 15;
        public int Top = 8;
        public int Width = 36;
        public int Height = 36;
        public int Interval = 60;
    }
    internal class FrmSubManager
    {
        private ToolTip tooltips = new ToolTip();
        private List<MenuModel> menus;
        private string path = Path.Combine(Appbus.BasePath, "Config\\FromManager.config");
        private Dictionary<string, SwitchMenu> dicMenu;
        private Dictionary<string, Form> dicForm;
        public FrmSubManager()
        {
            Load();
            dicMenu = new Dictionary<string, SwitchMenu>();
            dicForm = new Dictionary<string, Form>();
        }
        public void Register(string name, MenuLocation ctrlPos, System.Windows.Forms.Control menuParent, System.Windows.Forms.Control subFormParent, EventHandler eventClick)
        {
            if (string.IsNullOrEmpty(name))
                return;
            var menu = menus?.Where(p => p.Name == name).OrderBy(p => p.Index).FirstOrDefault();
            if (menu != null)
            {
                if (menu.Childs.Count > 0)
                {
                    //menu
                    int i = 0;
                    foreach (var item in menu.Childs)
                    {
                        SwitchMenu swBtn = new SwitchMenu();
                        swBtn.Name = item.Name;
                        if (ctrlPos != null)
                        {
                            swBtn.BackColor = SystemColors.Window;
                            swBtn.Width = ctrlPos.Width;
                            swBtn.Height = ctrlPos.Height;
                            swBtn.Left = ctrlPos.Left + ctrlPos.Interval * i;
                            swBtn.Top = ctrlPos.Top;
                            swBtn.Parent = menuParent;
                            swBtn.Tag = item.Name;
                            SwBtnSetBackImage(swBtn, item.UnCheckImage, item.CheckImage);
                        }
                        if (eventClick != null)
                            swBtn.Click += eventClick;
                        tooltips.SetToolTip(swBtn, item.Text);
                        dicMenu.Add(item.Name, swBtn);

                        CreateFrm(item.Name, subFormParent);

                        if (i == 0)
                        {
                            swBtn.Checked = true;
                            eventClick(swBtn, null);
                        }

                        i++;
                    }
                }
                else
                {
                    CreateFrm(menu.Name, subFormParent);
                }
            }
        }
        private void CreateFrm(string name, System.Windows.Forms.Control subFormParent)
        {
            try
            {
                Type type = Type.GetType(name);
                if (type != null)
                {
                    Form fm = (Form)Activator.CreateInstance(type);

                    fm.TopLevel = false;
                    fm.FormBorderStyle = FormBorderStyle.None;
                    fm.Parent = subFormParent;
                    fm.Dock = DockStyle.Fill;
                    dicForm.Add(name, fm);
                    fm.Show();
                    fm.Hide();
                }
                //Assembly ass = Assembly.Load(item.Name);
                //Form fm = (Form)ass.CreateInstance(item.Name);
            }
            catch (Exception ex)
            {
                string info = ex.Message;
            }
        }
        private void SwBtnSetBackImage(SwitchMenu swMenu, string unCheckImageName, string checkImageName)
        {
            if (!string.IsNullOrEmpty(unCheckImageName))
            {
                string unCheckImageFilePath = Path.Combine(Appbus.BasePath, "ImageSource\\" + unCheckImageName + ".png");
                if (File.Exists(unCheckImageFilePath))
                {
                    Bitmap unCheckImage = new Bitmap(unCheckImageFilePath);
                    swMenu.BackgroundImage = unCheckImage;
                    swMenu.UnCheckImage = unCheckImage;
                }
            }
            if (!string.IsNullOrEmpty(checkImageName))
            {
                string checkImageFilePath = Path.Combine(Appbus.BasePath, "ImageSource\\" + checkImageName + ".png");
                if (File.Exists(checkImageFilePath))
                {
                    Bitmap CheckImage = new Bitmap(checkImageFilePath);
                    swMenu.CheckImage = CheckImage;
                }
            }
        }
        public void Show(string frmName)
        {
            if (dicForm.ContainsKey(frmName))
            {
                if (dicForm[frmName].Visible)
                    return;

                foreach (var item in dicForm)
                {
                    item.Value.Visible = false;
                }

                var frm = dicForm[frmName];
                frm.Show();
            }
        }
        public void Load()
        {
            menus = JsonSerializeHelper.Load<List<MenuModel>>(path);
        }
    }
}
